/**
 * Created by KhoaNam on 6/24/2014.
 */

var StatusLayer = cc.Layer.extend({
    ctor: function(){
        this._super();
        this.inforBackgroundSprite = null;
        this.inforBoundingBox = null;

        this.botDirectionLabel = null;
        this.rightDirectionLabel = null;
        this.topDirectionLabel = null;
        this.leftDirectionLabel = null;

        this.botNameLabel = null;
        this.rightNameLabel = null;
        this.topNameLabel = null;
        this.leftNameLabel = null;

        this.botMoneyLabel = null;
        this.rightMoneyLabel = null;
        this.topMoneyLabel = null;
        this.leftMoneyLabel = null;
    },

    init: function(){
        this._super();
        var size = cc.Director.getInstance().getWinSize();
        this.inforBackgroundSprite = cc.Sprite.create(INFO.GAME.INFOBACKGROUND);
        this.inforBackgroundSprite.setPosition(size.width/2, size.height/2);
        this.addChild(this.inforBackgroundSprite);

        this.inforBoundingBox = this.inforBackgroundSprite.getBoundingBox();

        this.drawBotUserInfo();
        this.drawRightUserInfo();
        this.drawTopUserInfo();
        this.drawLeftUserInfo();

        //this.showDice();
    },

    drawBotUserInfo: function(){
        if(players[1] == undefined){
            return;
        }
        var size = cc.Director.getInstance().getWinSize();
        var x = size.width/2;
        var y = size.height/2 - this.inforBoundingBox.getHeight()/2 + 5;

        this.botNameLabel = cc.LabelTTF.create(players[1].name, INFO.PLAYER.NAME_FONT, 12);
        this.botNameLabel.setPosition(x,y + 5);
        this.addChild(this.botNameLabel);

        this.botMoneyLabel = cc.LabelTTF.create(players[1].money, INFO.PLAYER.NAME_FONT, 12);
        this.botMoneyLabel.setPosition(x, y + 16);
        this.addChild(this.botMoneyLabel);

        var widthName = this.botNameLabel.getBoundingBox().getWidth();
        var widthMoney = this.botMoneyLabel.getBoundingBox().getWidth();



        this.botDirectionLabel = cc.LabelTTF.create(INFO.TEXT.DIRECTION[players[1].position], INFO.PLAYER.NAME_FONT, 20);
        this.addChild(this.botDirectionLabel);
        this.botDirectionLabel.setPosition(x, y + 32);

    },

    drawRightUserInfo: function(){
        if(players[2] == undefined){
            return;
        }
        var size = cc.Director.getInstance().getWinSize();
        var x = size.width/2 + this.inforBoundingBox.getWidth()/2 - 5;
        var y = size.height/2;

        this.rightNameLabel = cc.LabelTTF.create(players[2].name, INFO.PLAYER.NAME_FONT, 12);
        this.rightNameLabel.setPosition(x - 5,y);
        this.addChild(this.rightNameLabel);

        this.rightMoneyLabel = cc.LabelTTF.create(players[2].money, INFO.PLAYER.NAME_FONT, 12);
        this.rightMoneyLabel.setPosition(x - 16, y);
        this.addChild(this.rightMoneyLabel);

        var widthName = this.rightNameLabel.getBoundingBox().getWidth();
        var widthMoney = this.rightMoneyLabel.getBoundingBox().getWidth();



        this.rightDirectionLabel = cc.LabelTTF.create(INFO.TEXT.DIRECTION[players[2].position], INFO.PLAYER.NAME_FONT, 20);
        this.addChild(this.rightDirectionLabel);
        this.rightDirectionLabel.setPosition(x - 32, y);

        var rotate1 = cc.RotateTo.create(0, -90);
        var action1 = cc.Sequence.create(rotate1);

        var rotate2 = cc.RotateTo.create(0, -90);
        var action2 = cc.Sequence.create(rotate2);

        var rotate3 = cc.RotateTo.create(0, -90);
        var action3 = cc.Sequence.create(rotate3);
        this.rightNameLabel.runAction(action1);
        this.rightMoneyLabel.runAction(action2);
        this.rightDirectionLabel.runAction(action3);
    },

    drawTopUserInfo: function(){
        if(players[3] == undefined){
            return;
        }
        var size = cc.Director.getInstance().getWinSize();
        var x = size.width/2;
        var y = size.height/2 + this.inforBoundingBox.getHeight()/2 - 5;

        this.topNameLabel = cc.LabelTTF.create(players[3].name, INFO.PLAYER.NAME_FONT, 12);
        this.topNameLabel.setPosition(x,y - 5);
        this.addChild(this.topNameLabel);

        this.topMoneyLabel = cc.LabelTTF.create(players[3].money, INFO.PLAYER.NAME_FONT, 12);
        this.topMoneyLabel.setPosition(x, y - 16);
        this.addChild(this.topMoneyLabel);

        var widthName = this.topNameLabel.getBoundingBox().getWidth();
        var widthMoney = this.topMoneyLabel.getBoundingBox().getWidth();



        this.topDirectionLabel = cc.LabelTTF.create(INFO.TEXT.DIRECTION[players[3].position], INFO.PLAYER.NAME_FONT, 20);
        this.addChild(this.topDirectionLabel);
        this.topDirectionLabel.setPosition(x, y - 32);

        var rotate1 = cc.RotateTo.create(0, 180);
        var action1 = cc.Sequence.create(rotate1);

        var rotate2 = cc.RotateTo.create(0, 180);
        var action2 = cc.Sequence.create(rotate2);

        var rotate3 = cc.RotateTo.create(0, 180);
        var action3 = cc.Sequence.create(rotate3);
        this.topNameLabel.runAction(action1);
        this.topMoneyLabel.runAction(action2);
        this.topDirectionLabel.runAction(action3);
    },

    drawLeftUserInfo: function(){
        if(players[4] == undefined){
            return;
        }
        var size = cc.Director.getInstance().getWinSize();
        var x = size.width/2 - this.inforBoundingBox.getWidth()/2 + 5;
        var y = size.height/2;

        this.leftNameLabel = cc.LabelTTF.create(players[4].name, INFO.PLAYER.NAME_FONT, 12);
        this.leftNameLabel.setPosition(x + 5,y);
        this.addChild(this.leftNameLabel);

        this.leftMoneyLabel = cc.LabelTTF.create(players[4].money, INFO.PLAYER.NAME_FONT, 12);
        this.leftMoneyLabel.setPosition(x + 16, y);
        this.addChild(this.leftMoneyLabel);

        var widthName = this.leftNameLabel.getBoundingBox().getWidth();
        var widthMoney = this.leftMoneyLabel.getBoundingBox().getWidth();



        this.leftDirectionLabel = cc.LabelTTF.create(INFO.TEXT.DIRECTION[players[4].position], INFO.PLAYER.NAME_FONT, 20);
        this.addChild(this.leftDirectionLabel);
        this.leftDirectionLabel.setPosition(x + 32, y);

        var rotate1 = cc.RotateTo.create(0, 90);
        var action1 = cc.Sequence.create(rotate1);

        var rotate2 = cc.RotateTo.create(0, 90);
        var action2 = cc.Sequence.create(rotate2);

        var rotate3 = cc.RotateTo.create(0, 90);
        var action3 = cc.Sequence.create(rotate3);
        this.leftNameLabel.runAction(action1);
        this.leftMoneyLabel.runAction(action2);
        this.leftDirectionLabel.runAction(action3);
    },

    doDice: function(pos, callback){
        var size = cc.Director.getInstance().getWinSize();
        cc.SpriteFrameCache.getInstance().addSpriteFrames(INFO.DICE.PLIST);

        if(pos == undefined){
            pos = 1;
        }

        var  spriteSheet = cc.SpriteBatchNode.create("res/dice/dice.png");
        var  spriteSheet2 = cc.SpriteBatchNode.create("res/dice/dice.png");
        var diceSprite = cc.Sprite.createWithSpriteFrameName("sprite1.png");
        var diceSprite2 = cc.Sprite.createWithSpriteFrameName("sprite15.png");


        var animFrames2 = [];
        for (var i = 15; i >= 1; i--) {
            var str = "sprite" + i + ".png";
            var frame = cc.SpriteFrameCache.getInstance().getSpriteFrame(str);
            animFrames2.push(frame);
        }

        var animFrames = [];
        for (var i = 1; i <= 15; i++) {
            var str = "sprite" + i + ".png";
            var frame = cc.SpriteFrameCache.getInstance().getSpriteFrame(str);
            animFrames.push(frame);
        }

        var distanceDice = 60;

        switch (pos){
            case 1:
                diceSprite.setPosition(cc.p(size.width/2 - distanceDice/2, size.height /2 - this.inforBoundingBox.getHeight()/2 - distanceDice));
                diceSprite2.setPosition(cc.p(size.width/2 + distanceDice/2, size.height /2 - this.inforBoundingBox.getHeight()/2 - distanceDice));
                break;
            case 2:
                diceSprite.setPosition(cc.p(size.width/2 + this.inforBoundingBox.getWidth()/2 + distanceDice - distanceDice/2, size.height /2));
                diceSprite2.setPosition(cc.p(size.width/2 + this.inforBoundingBox.getWidth()/2 + distanceDice + distanceDice/2, size.height /2));
                break;
            case 3:
                diceSprite.setPosition(cc.p(size.width/2 - distanceDice/2, size.height /2 + this.inforBoundingBox.getHeight()/2 + distanceDice));
                diceSprite2.setPosition(cc.p(size.width/2 + distanceDice/2, size.height /2 + this.inforBoundingBox.getHeight()/2 + distanceDice));
                break;
            case 4:
                diceSprite.setPosition(cc.p(size.width/2 - this.inforBoundingBox.getWidth()/2 - distanceDice + distanceDice/2, size.height /2));
                diceSprite2.setPosition(cc.p(size.width/2 - this.inforBoundingBox.getWidth()/2 - distanceDice - distanceDice/2, size.height /2));
                break;
        }

        var animation = cc.Animation.create(animFrames, 0.1);
        var runningAction = cc.RepeatForever.create(cc.Animate.create(animation));
        var animation2 = cc.Animation.create(animFrames2, 0.1);
        var runningAction2 = cc.RepeatForever.create(cc.Animate.create(animation2));

        var addNewDice = function(that){

            var  newDice1 = new MyTileNoEvent("res/dice/dice" +  that.getParent().dice1 + ".png");
            var  newDice2 = new MyTileNoEvent("res/dice/dice" +  that.getParent().dice2 + ".png");

            newDice1.setPosition(cc.p(diceSprite.getPositionX(), diceSprite.getPositionY()));
            newDice2.setPosition(cc.p(diceSprite2.getPositionX(), diceSprite2.getPositionY()));

            that.addChild(newDice1, 1000);
            that.addChild(newDice2, 1000);

            that.scheduleOnce(function(){
                var size = cc.Director.getInstance().getWinSize();

                var move1 = cc.MoveTo.create(0.5, cc.p(size.width/2 - 15, size.height/2));
                var move2 = cc.MoveTo.create(0.5, cc.p(size.width/2 + 15, size.height/2));

                var scale1 = cc.ScaleTo.create(0.5, 0.7, 0.7);
                var scale2 = cc.ScaleTo.create(0.5, 0.7, 0.7);

                newDice1.runAction(cc.Sequence.create(move1));
                newDice2.runAction(cc.Sequence.create(move2));

                newDice1.runAction(cc.Sequence.create(scale1));
                newDice2.runAction(cc.Sequence.create(scale2));


            }, 3);

        };


        var ranNumber = Math.floor((Math.random() * 10) + 1);
        if(ranNumber % 2 == 0){
            var jump =  cc.JumpBy.create(0.5, cc.p(0, 0), 100, 1);
            diceSprite.runAction(runningAction);
            diceSprite.runAction(cc.Sequence.create(jump,cc.DelayTime.create(0.3),  cc.CallFunc.create(function(){
                diceSprite.pauseSchedulerAndActions();
                this.removeChild(spriteSheet);
            }, this)));
            this.addChild(spriteSheet, 1000);

            var jump2 =  cc.JumpBy.create(0.5, cc.p(0, 0), 80, 1);
            diceSprite2.runAction(runningAction2);
            diceSprite2.runAction(cc.Sequence.create(jump2, cc.DelayTime.create(0.3),  cc.CallFunc.create(function(){
                if(callback){
                    callback(this);
                }
                diceSprite2.pauseSchedulerAndActions();
                addNewDice(this);
                this.removeChild(spriteSheet2);

            }, this)));
            diceSprite2.setPositionY(diceSprite2.getPositionY() - 20);
            this.addChild(spriteSheet2, 1000);
        }
        else{
            var jump =  cc.JumpBy.create(0.5, cc.p(0, 0), 80, 1);
            diceSprite.runAction(runningAction);
            diceSprite.runAction(cc.Sequence.create( jump, cc.DelayTime.create(0.3) ,cc.CallFunc.create(function(){
                if(callback){
                    callback(this);
                }
                diceSprite.pauseSchedulerAndActions();
                this.removeChild(spriteSheet);
                addNewDice(this);
            }, this)));

            this.addChild(spriteSheet, 1000);
            diceSprite.setPositionY(diceSprite.getPositionY() - 20);

            var jump2 =  cc.JumpBy.create(0.5, cc.p(0, 0), 100, 1);
            diceSprite2.runAction(runningAction2);
            diceSprite2.runAction(cc.Sequence.create(jump2, cc.DelayTime.create(0.3), cc.CallFunc.create(function(){
                diceSprite2.pauseSchedulerAndActions();
                this.removeChild(spriteSheet2);
            }, this)));
            this.addChild(spriteSheet2, 1000);
        }

        spriteSheet.addChild(diceSprite);
        spriteSheet2.addChild(diceSprite2);
    },

    showDice: function(){
        var dice1 = cc.Sprite.create("res/dice/" + this.getParent().dice1 + ".png");
        var dice2 = cc.Sprite.create("res/dice/" + this.getParent().dice2 + ".png");
        var size = cc.Director.getInstance().getWinSize();
        this.addChild(dice1);
        this.addChild(dice2);

        dice1.setPosition(size.width/2 - 15, size.height/2);
        dice2.setPosition(size.width/2 + 15, size.height/2);
    },

    setTurn: function(pos){
        if(this.botDirectionLabel != null){
            this.botDirectionLabel.setColor(cc.WHITE);
        }
        if(this.rightDirectionLabel != null){
            this.rightDirectionLabel.setColor(cc.WHITE);
        }
        if(this.topDirectionLabel != null){
            this.topDirectionLabel.setColor(cc.WHITE);
        }
        if(this.leftDirectionLabel != null){
            this.leftDirectionLabel.setColor(cc.WHITE);
        }

        switch (pos){
            case 1:
                if(this.botDirectionLabel != null)
                    this.botDirectionLabel.setColor(cc.RED);
                break;
            case 2:
                if(this.rightDirectionLabel != null)
                    this.rightDirectionLabel.setColor(cc.RED);
                break;
            case 3:
                if(this.topDirectionLabel != null)
                    this.topDirectionLabel.setColor(cc.RED);
                break;
            case 4:
                if(this.leftDirectionLabel != null)
                    this.leftDirectionLabel.setColor(cc.RED);
                break;
        }
    }
});
